<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Omnipay\Omnipay;

class TestPaymentController extends Controller
{
    public function getCard(Request $request)
    {
        $secret = $request->has('secret') ? $request->input('secret') : '';
        if($secret)
        {
            $encrypt = _encrypt($secret, 'DECODE', '#df@%sd');
            if($encrypt)
            {
                $orderData = explode('|', $encrypt);
                if(count($orderData) == 2)
                {
                    $orderID = $orderData[0];
                    $money = $orderData[1];
                    return view('stripe-dev')->with(['orderID' => $orderID, 'money' => $money, 'secret' => $secret]);
                }
            }
        }
        return view('errors.404');
    }


    public function postPayStripe(Request $request)
    {
        $code = $request->has('code') ? $request->input('code') : '';
        $stripeToken = $request->has('stripeToken') ? $request->input('stripeToken') : '';
        if ($code)
        {
            $encrypt = _encrypt($code, 'DECODE', '#df@%sd');
            if($encrypt)
            {
                $orderData = explode('|', $encrypt);
                if(count($orderData) == 2)
                {
                    $orderID = $orderData[0];
                    $money = $orderData[1];
                    /*
                * 统一下单
                */
                    $stripe = Omnipay::create('Stripe');
                    $stripe->setApiKey('sk_test_5YmnvF9nFBiIkIvRmaToLDIU');

                    $response = $stripe->purchase([
                        'amount' => round($money,0),
                        'currency' => 'JPY',
                        'token' => $stripeToken,
                        'metadata'  =>  [
                            'orderID'    =>  $orderID
                        ]
                    ])->send();

                    if ($response->isRedirect()) {
                        // redirect to offsite payment gateway
                        $response->redirect();
                    } elseif ($response->isSuccessful()) {
                        // payment was successful: update database
                        $backData = [
                            'code'      =>  1,
                            'msg'       =>  'success'
                        ];
                        return json_encode($backData);
                    } else {
                        // payment failed: display message to customer
                        $backData = [
                            'code'      =>  0,
                            'msg'       =>  $response->getMessage()
                        ];

                        return json_encode($backData);
                    }
                }
            }
        }
        $backData = [
            'code'      =>  0,
            'msg'       =>  'Error'
        ];

        return json_encode($backData);
    }
}
